package cn.cnhis.online.activity.main.model;

import android.util.Log;

import java.io.File;
import java.util.List;
import java.util.Map;

import cn.cnhis.online.BuildConfig;
import cn.cnhis.online.activity.main.contract.IMainContract;
import cn.cnhis.online.base.DefaultDisposablePoolImpl;
import cn.cnhis.online.mymodule.HttpController;
import io.reactivex.internal.observers.BlockingBaseObserver;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

/**
 * @author 刘祥龙
 * @describe
 * @date 2020/2/28  15:38
 * - generate by MvpAutoCodePlus plugin.
 */

public class MainModel extends DefaultDisposablePoolImpl implements IMainContract.Model {


    @Override
    public void getAppVersion(String version, IMainContract.View view) {
        HttpController.getAppVersion(version, new BlockingBaseObserver<Map>() {
            @Override
            public void onNext(Map map) {
                view.getAppVersion(map);
            }

            @Override
            public void onError(Throwable e) {
                if (BuildConfig.DEBUG) Log.d("MainModel", "e:" + e);
                view.showErr("服务异常请联系管理员！");
            }
        });
    }

    @Override
    public void getMenuShowColor(IMainContract.View view) {
        HttpController.getMenuShowColor(null, new BlockingBaseObserver<Map>() {
            @Override
            public void onNext(Map map) {
                view.getMenuShowColor(map);
            }

            @Override
            public void onError(Throwable e) {
                if (BuildConfig.DEBUG) Log.d("MainModel", "e:" + e);
                view.showErr("服务异常请联系管理员！");
            }
        });
    }

    @Override
    public void loadFile(File file, IMainContract.View view) {

        MultipartBody.Builder builder = new MultipartBody.Builder()
                .setType(MultipartBody.FORM);

        //2.获取图片，创建请求体
//        RequestBody body=RequestBody.create(MediaType.parse("multipart/form-data"),file_name);//表单类型
        RequestBody body=RequestBody.create(MediaType.parse("file/*"),file);//表单类型

        //3.调用MultipartBody.Builder的addFormDataPart()方法添加表单数据
        builder.addFormDataPart("file_data",file.getName(),body); //添加图片数据，body创建的请求体

        //4.创建List<MultipartBody.Part> 集合，
        //  调用MultipartBody.Builder的build()方法会返回一个新创建的MultipartBody
        //  再调用MultipartBody的parts()方法返回MultipartBody.Part集合
        List<MultipartBody.Part> parts = builder.build().parts();

        HttpController.loadFile(parts, new BlockingBaseObserver<Map>() {
            @Override
            public void onNext(Map map) {
                view.loadFile(map);
            }

            @Override
            public void onError(Throwable e) {
                if (BuildConfig.DEBUG) Log.d("MainModel", "e:" + e);
                view.showErr("服务异常请联系管理员！");
            }
        });
    }
}

